﻿import core.hgeall;


const SCREEN_WIDTH  =800;
const SCREEN_HEIGHT =600;

const MIN_OBJECTS	=100;
const MAX_OBJECTS  =2000;

struct sprObject {

    float x,y;
    float dx,dy;
    float scale,rot;
    float dscale,drot;
    uint color;
}

sprObject[]	pObjects;
int			nObjects;
int			nBlend;


// Resource handles

HTEXTURE			tex, bgtex;
hgeSprite			spr, bgspr;
hgeFontCN				fnt;

// Set up blending mode for the scene

void SetBlend(int blend) {
    static int sprBlend[5]=[
                               BLEND_COLORMUL | BLEND_ALPHABLEND | BLEND_NOZWRITE,
                               BLEND_COLORADD | BLEND_ALPHABLEND | BLEND_NOZWRITE,
                               BLEND_COLORMUL | BLEND_ALPHABLEND | BLEND_NOZWRITE,
                               BLEND_COLORMUL | BLEND_ALPHAADD   | BLEND_NOZWRITE,
                               BLEND_COLORMUL | BLEND_ALPHABLEND | BLEND_NOZWRITE];

    static uint[5] fntColor=
        [
            0xFFFFFFFF, 0xFF000000, 0xFFFFFFFF, 0xFF000000, 0xFFFFFFFF
        ];

    static uint[5][5] sprColors=
        [
            [ 0xFFFFFFFF, 0xFFFFE080, 0xFF80A0FF, 0xFFA0FF80, 0xFFFF80A0 ],
            [ 0xFF000000, 0xFF303000, 0xFF000060, 0xFF006000, 0xFF600000 ],
            [ 0x80FFFFFF, 0x80FFE080, 0x8080A0FF, 0x80A0FF80, 0x80FF80A0 ],
            [ 0x80FFFFFF, 0x80FFE080, 0x8080A0FF, 0x80A0FF80, 0x80FF80A0 ],
            [ 0x40202020, 0x40302010, 0x40102030, 0x40203010, 0x40102030 ]
        ];

    if (blend>4) blend=0;
    nBlend=blend;

    spr.SetBlendMode(sprBlend[blend]);

    fnt.SetColor(fntColor[blend]);

    for (int i=0;i<MAX_OBJECTS;i++) pObjects[i].color=sprColors[blend][hge.Random_Int(0,4)];
}













ifHge			hge;


float dx,dy;
extern(C) {
    bool frameFunc() {
        float dt=hge.Timer_GetDelta();
        static ulong cc;

        cc++;

        int i;

        // Process keys
//if ( hge.Input_GetKeyState( HGEK_ESCAPE)) return true;

        switch (hge.Input_GetKey()) {
        case HGEK_UP:
            if (nObjects<MAX_OBJECTS) nObjects+=100;
            break;
        case HGEK_DOWN:
            if (nObjects>MIN_OBJECTS) nObjects-=100;
            break;
        case HGEK_SPACE:
            SetBlend(++nBlend);
            break;
        case HGEK_ESCAPE:
            return true;
            break;
        default:
            break;
        }

        // Update the scene

        for (i=0;i<nObjects;i++) {
            pObjects[i].x+=pObjects[i].dx*dt;
            if (pObjects[i].x>SCREEN_WIDTH || pObjects[i].x<0) pObjects[i].dx=-pObjects[i].dx;
            pObjects[i].y+=pObjects[i].dy*dt;
            if (pObjects[i].y>SCREEN_HEIGHT || pObjects[i].y<0) pObjects[i].dy=-pObjects[i].dy;
            pObjects[i].scale+=pObjects[i].dscale*dt;
            if (pObjects[i].scale>2 || pObjects[i].scale<0.5) pObjects[i].dscale=-pObjects[i].dscale;
            pObjects[i].rot+=pObjects[i].drot*dt;
        }

        return false;
    }
    bool renderFunc() {
        int i;

        // Render the scene

        hge.Gfx_BeginScene();
        bgspr.Render(0,0);

        for (i=0;i<nObjects;i++) {
            spr.SetColor(pObjects[i].color);
            spr.RenderEx(pObjects[i].x, pObjects[i].y, pObjects[i].rot, pObjects[i].scale);
        }

        fnt.printf(7, 7, HGETEXT_LEFT, "UP and hares: %d\nSPACE to change blending mode: %d\nFPS: %d", nObjects, nBlend, hge.Timer_GetFPS());
		 fnt.printf(7,50,HGETEXT_LEFT,"obj数量= %d ,  工 fps= %d", nObjects, hge.Timer_GetFPS());
        hge.Gfx_EndScene();

        return false;
    }

}

void main() {
    int i;
    gameInfo gi= gameInfo.getInstance();
    hge= gi.hge();

    gi.setVideo(800,600,32);
    gi.setFunc(&frameFunc,&renderFunc);
    gi.setStr("me","me.log");
    hge.System_SetStateBool(hgeBoolState.HGE_USESOUND, false);

    try {
        gi.init;
        // Load textures

        bgtex	=gi.res.tex_load(r"res\bg2.png");
        tex		=gi.res.tex_load(r"res\zazaka.png");
        // Load font, create sprites
        fnt =new hgeFontCN(r"..\test3\font_cn.sbin");

        spr=new hgeSprite(tex,0,0,64,64);
        spr.SetHotSpot(32,32);

        bgspr=new hgeSprite(bgtex,0,0,800,600);
        bgspr.SetBlendMode(BLEND_COLORADD | BLEND_ALPHABLEND | BLEND_NOZWRITE);
        bgspr.SetColor(0xFF000000,0);
        bgspr.SetColor(0xFF000000,1);
        bgspr.SetColor(0xFF000040,2);
        bgspr.SetColor(0xFF000040,3);

        // Initialize objects list

        pObjects=new sprObject[MAX_OBJECTS];
        nObjects=1000;

        for (i=0;i<MAX_OBJECTS;i++) {
            pObjects[i].x=hge.Random_Float(0,SCREEN_WIDTH);
            pObjects[i].y=hge.Random_Float(0,SCREEN_HEIGHT);
            pObjects[i].dx=hge.Random_Float(-200,200);
            pObjects[i].dy=hge.Random_Float(-200,200);
            pObjects[i].scale=hge.Random_Float(0.5f,2.0f);
            pObjects[i].dscale=hge.Random_Float(-1.0f,1.0f);
            pObjects[i].rot=hge.Random_Float(0,M_PI*2);
            pObjects[i].drot=hge.Random_Float(-1.0f,1.0f);
        }

        SetBlend(0);

        hge.System_Start();
        mes("启动成功!");
        // Delete created objects and free loaded resources

    } catch (Exception e) {
        mes("程序异常: ",e);
    } finally {
        delete gi;
    }

}